package com.jijidom.product.controller;

import com.jijidom.common.DecreaseStockInput;
import com.jijidom.product.VO.ProductInfoVO;
import com.jijidom.product.VO.ProductVO;
import com.jijidom.product.VO.ResultVO;
import com.jijidom.product.dataobject.ProductCategory;
import com.jijidom.product.dataobject.ProductInfo;
import com.jijidom.product.repository.ProductCategoryRepository;
import com.jijidom.product.service.ProductService;
import com.jijidom.product.utils.ResultVOUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName ProductController
 * @Description TODO
 * @Author JinTao
 * @Date 2018/8/30 17:11
 * @Version 1.0
 **/
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productInfoRepository;

    @Autowired
    private ProductCategoryRepository categoryRepository;

    /**
     * 1.查询所有在架商品
     * 2.查询类目type列表
     * 3.查询类目
     * 4.构造函数
     * @return
     */
	@GetMapping("/list")
    public ResultVO<Object> lsit(){
        //查询所有在架的商品
        List<ProductInfo> products = productInfoRepository.findUpAll();
        //获取类目type列表
        List<Integer> categoryTypeList = products.stream().map(ProductInfo::getCategoryType).collect(Collectors.toList());
        //从数据库查询类目
        List<ProductCategory> productCategories = categoryRepository.findByCategoryType(categoryTypeList);
        //构造数据
        List<ProductVO> productVOS = new ArrayList<>();
        for(ProductCategory li:productCategories){
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(li.getCategoryName());
            productVO.setCategoryType(li.getCategoryType());
            List<ProductInfoVO> productInfoVOS = new ArrayList<>();
            for (ProductInfo in:products){
                if(!in.getCategoryType().equals(li.getCategoryType())){
                    continue;
                }
                ProductInfoVO productInfoVO = new ProductInfoVO();
                BeanUtils.copyProperties(in,productInfoVO);
                productInfoVOS.add(productInfoVO);
            }
            productVO.setProductInfoVOS(productInfoVOS);
            productVOS.add(productVO);
        }
        return ResultVOUtil.success(productVOS);
    }

    @GetMapping("/msg")
    public String msg(){
        return "这是一个服务1";
    }

    /**
     * 获取商品列表(给订单服务用的)
     * @param productIdList
     * @return
     */
    @PostMapping("/ListForOrder")
    public List<ProductInfo> listForOrder(@RequestBody List<String> productIdList){
        System.out.println(productIdList);
        return productInfoRepository.findByProductIdIn(productIdList);
    }

    @PostMapping("/decreaseStock")
    public void decreaseStock(@RequestBody List<DecreaseStockInput> carDTOList){
        productInfoRepository.decreaseStock(carDTOList);
    }
}
